import java.util.Collections;
import java.util.HashMap;
import ws.*;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;


public class ListMessagesBean {
    
    private Pmui selected;

    public ListMessagesBean() {
    }
    
    public List<Pmui> getReceivedMessages(){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user =(UserUI) session.getAttribute("User");
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        return port.getReceivedPrivateMessages(user.getId());
    }
    
    public List<Pmui> getSentMessages(){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user =(UserUI) session.getAttribute("User");
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();    
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        return port.getSentPrivateMessages(user.getId());
    }
    
    public Pmui getSelected() {
        return selected;
    }

    public void setSelected(Pmui selected) {
        this.selected = selected;
    }

    public void selectionListener(ActionEvent event) {
        this.selected = (Pmui) event.getComponent().getAttributes().get("selection");
        FacesContext context = FacesContext.getCurrentInstance();
        ReadMessageBean readMessageBean = (ReadMessageBean) context.getApplication().evaluateExpressionGet(context, "#{readMessageBean}", ReadMessageBean.class);
        readMessageBean.setSelected(selected);
    }
    
    public String goToDetails(){
        return "go";
    }
}
